// pages/order/order.js
import {
  generateOrder,
  userAddressList
} from '../../api/api'
import {
  region
} from '../../api/region'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {},
    address: {},
    total: 0.1
  },
  //设置scroll-view高度
  setHeight() {
    let top = app.globalData.navHeight + 5
    this.setData({
      height: app.globalData.windowHeight - top
    })
  },
  //选择收货地址
  chooseAddress(){
    wx.navigateTo({
      url: '/pages/address/address?page=choose&&obj='+JSON.stringify(this.data.info),
    })
  },
  //生成订单
  createdOrder() {
    generateOrder({
      token: app.globalData.userInfo.token,
      address_id: this.data.address.address_id,
      money: this.data.total,
      shopping_car_ids: this.data.info.shopping_car_id

    }).then(res => {
      console.log(res);
      wx.requestPayment({
        timeStamp: res.data.timeStamp,
        nonceStr: String(res.data.nonce_str),
        package: `prepay_id=${res.data.prepay_id}`,
        signType: 'MD5',
        paySign: String(res.data.paySign),
        success (res) {
          console.log(res);
          wx.navigateTo({
            url: '/pages/orderList/orderList?id=-1',
          })
         },
        fail (res) { 
          console.log(res);
        }
      })
    })
  },

  //获取地址列表
  getAddress() {
    userAddressList({
      token: app.globalData.userInfo.token
    }).then(res => {
      res.data.forEach(item => {
        let procince = `${item.procince}0000`
        let city = `${item.procince}${item.city}00`
        let area = `${item.procince}${item.city}${item.area}`
        let index = region.findIndex(i => i.provinceCode == procince)
        let cIndex = region[index].mallCityList.findIndex(j => j.cityCode == city)
        let aIndex = region[index].mallCityList[cIndex].mallAreaList.findIndex(k => k.areaCode == area)
        item.procince_text = region[index].provinceName
        item.city_text = region[index].mallCityList[cIndex].cityName
        item.area_text = region[index].mallCityList[cIndex].mallAreaList[aIndex].areaName
      })
      let address = res.data.filter(item => item.default)
      this.setData({
        address: address[0]
      })
      console.log(this.data.address);
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setHeight()
    if(options.item){
      console.log(options.item);
      this.setData({
        address:JSON.parse(options.item)
      })
    }else{
      this.getAddress()
    }
    let info=JSON.parse(options.obj)
    let total=0
    info.chooseCommodityArr.forEach(item=>{
      total+=Number(item.money)
    })
    this.setData({
      info,
      total
    })
    console.log(this.data.info);

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})